package com.yvon.maple.service.system.controller;

import com.yvon.boot.common.result.ResponseData;
import com.yvon.maple.cloud.web.data.AbstractController;
import com.yvon.maple.service.system.dto.DeptDTO;
import com.yvon.maple.service.system.entity.Dept;
import com.yvon.maple.service.system.service.DeptService;
import com.yvon.maple.service.system.vo.DeptVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping(value = "/depts")
@Api(value = "部门管理", tags = "系统服务 - 部门管理API")
public class DeptController extends AbstractController<Dept, DeptDTO, DeptVO, DeptService> {


    @PostMapping
    @ApiOperation(value = "新增", notes = "新增API")
    public ResponseData<Dept> add(@RequestBody Dept vo) {
        getService().saveUpdateNode(vo);
        return success(vo);
    }


    @DeleteMapping
    @ApiOperation(
            value = "删除",
            notes = "删除API"
    )
    public ResponseData deleteByIds(@RequestBody List<String> ids) {
        return result(getService().removeByIds(ids));
    }

    @GetMapping({"/id/{id}"})
    @ApiOperation(
            value = "根据编号查询信息",
            notes = "根据编号查询信息API"
    )
    public ResponseData<Dept> getById(@PathVariable("id") String id) {
        Dept entity = getService().getById(id);
        return success(entity);
    }



    /**
     * 获取当前父节点下最大编号
     *
     * @param parentId
     * @return ResponseData<Integer>
     * @author : Yvon / 2022-03-14
     */
    @GetMapping("/findMaxSort/{parentId}")
    public ResponseData<Integer> findMaxSort(@PathVariable("parentId") Long parentId) {
        return success(getService().getMaxSort(parentId));
    }

    /**
     * 根据层级展开部门树表格
     *
     * @param level 展开层级
     * @param id    起始展开ID
     * @return ResponseData
     * @author : Yvon / 2022-03-14
     */
    @GetMapping("/list/{level}/{id}")
    public ResponseData list(@PathVariable("level") @NotEmpty int level, @PathVariable("id") Long id) {
        if (level == 0) {
            level = 2;
        }
        //默认为根节点
        if (Objects.isNull(id)) {
            id = 0L;
        }
        List<Dept> menus = getService().listDataByLevel(level, id);
        return ResponseData.success(menus);
    }
}
